<template>
  <el-dialog
    top="10vh"
    width="500px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
import getLangForm from "@/utils/langForm";
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'stock',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        type: 1,
        device_id: 0,
        number: '',
        project: '',
        department_id: 0,
        in_time: '',
        in_count: '',
      },
      rules: {
      },
      formList: [
        /**
         * 序号	设备/备件名称	设备/备件编号	工程项目名称	部门	入库时间	入库数量	出库时间	出库数量	结存数量
         *
         */
          // 创建出入库表单
          {
            prop: 'device_id',
            label: '设备/备件名称',
            type: 'select',
            options: [],
            colSpan: 12
          },
        {
          prop: 'number',
          label: '设备/备件编号',
          type: 'input',
          disabled: true,
          colSpan: 12
        },
        {
          prop: 'project',
          label: '工程项目名称',
          type: 'input',
          colSpan: 12
        },
        {
          prop: 'department_id',
          label: '部门',
          type: 'select',
          options: [],
          colSpan: 12
        },
        {
          prop: 'in_time',
          label: '入库时间',
          type: 'datetime',
          colSpan: 12
        },
        {
          prop: 'in_count',
          label: '入库数量',
          type: 'input',
          colSpan: 12
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
      http.device.list().then(res => {
        this.formList[0].options = res.data.map(item => {
          return {
            name: item.name,
            value: item.id
          }
        })
      })
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
